<template>
    <div>
        <a-select :filter-option="filterOption" :value="lineId" placeholder="选择路线" show-search @change="handleChange">
            <a-select-option v-for="(item,index) in lineList" :key="'lineId'+index" :value="item.value">
                {{item.label}}
            </a-select-option>
        </a-select>
    </div>
</template>

<script>
import { setDataForLabel } from '../../utils/util'
import { getAction } from '@api/manage'

export default {
    name: 'SelectLineList',
    props:{
        // 是否初始化数据
        isInitData : {
            type : Boolean,
            default : false
        },
        linesId : {
            type : String,
            default : ''
        }
    },
    data() {
        return {
            lineId : '',
            lineList: [],
        }
    },
    created() {
        this.initData()
    },
    watch:{
        linesId(){
            this.lineId = this.linesId;
        }
    },
    methods: {
        // 初始化查询数据
        async initData() {
            // 根据父组件传过来的值来查询
            let params = {
                userId : this.linesId
            }
            let list = await getAction('/hc/line/getRecycleLine',params)
            this.lineList = setDataForLabel(list.result, 'id', 'line_name');
            // if(this.lineList.length){
            //     let info = this.lineList[0];
            //     if(info){
            //         this.lineId = info.value;
            //         this.$emit('change',info.value,info,null);
            //     }
            // }
        },
        filterOption: function(input, option) {
            return (
                option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
            )
        },
        handleChange : function(value,option) {
            this.lineId = value;
            let info = this.lineList.find((val) => val.value == value);
            this.$emit('change',value,info,option);
        }
    }
}
</script>

<style scoped>

</style>
